package com.loooooo.pm2024.modules.pm.controller;


import com.loooooo.pm2024.data.Result;
import com.loooooo.pm2024.modules.pm.model.Attachment;
import com.loooooo.pm2024.modules.pm.selector.AttachmentSelector;
import com.loooooo.pm2024.modules.pm.selector.AttendanceSelector;
import com.loooooo.pm2024.modules.pm.service.AttachmentService;
import com.loooooo.pm2024.modules.pm.service.AttendanceService;
import com.loooooo.pm2024.modules.pm.service.TaskLogService;
import com.loooooo.pm2024.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/pm/attendance/clock")
public class AttendanceController {
    @Autowired
    AttendanceService attendanceService;

    @GetMapping(value = "/list")
    String listGet() {
        return "pm/pm.attendance.clock.list";

    }

    @PostMapping(value = "/list", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result listPost(@RequestBody(required = false) AttendanceSelector selector) {
        if (selector == null) selector = new AttendanceSelector();

        if (selector.getPageSize() != null) {
            if (selector.getPageNo() == null || selector.getPageNo() < 1) {
                selector.setPageNo(1);
            }
            Result result = new Result();
            int count = attendanceService.count(selector);
            result.set("total", count);
            if (count > (selector.getPageSize() * (selector.getPageNo() - 1))) {
                result.setData(attendanceService.list(selector));
            }
            return result.set("now", System.currentTimeMillis());
        }
        return Result.success(attendanceService.list(selector)).set("now", System.currentTimeMillis());

    }

    //
    @PostMapping(value = "/{type}")
    @ResponseBody
    Result update(@PathVariable("type") String type) {
        int r = attendanceService.clock(type);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());
    }

}
